package dao.impl;

import dao.IBookDao;
import pojo.Book;
import util.DBConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class BookDaoImpl implements IBookDao {

	private Connection conn = null;
	private PreparedStatement ps;
	String url = "jdbc:mysql://localhost:3306/javaeelab";
	// 查询所有图书
	public List<Book> queryAll() throws Exception {
		String sql = "select * from tb_book";
		List<Book> books=new ArrayList<Book>();
		conn=DBConn.getConnection(url);
		ps=conn.prepareStatement(sql);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			Book book=new Book();
			book.setId(rs.getInt("id"));
			book.setName(rs.getString("name"));
			book.setAuthor(rs.getString("author"));
			book.setPrice(rs.getFloat("price"));
			book.setPress(rs.getString("press"));
			book.setISBN(rs.getString("ISBN"));
			book.setPic(rs.getString("pic"));
			books.add(book);
		}
		return books;
	}

	public static void main(String[] args) throws Exception {
		IBookDao bookDao=new BookDaoImpl();
		List<Book> books=bookDao.queryAll();
		for (Book book:books){
			System.out.println(book);
		}

	}
}
